//
//  UIApplication+Manager.m
//  高仿暴走斗图
//
//  Created by key on 16/4/26.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "UIApplication+Manager.h"
#import "HSYBaseViewController+Alert.h"

@implementation UIApplication (Manager)

+ (UIApplication *)application {
    
    return [UIApplication sharedApplication];
}

#pragma mark Open System Resources

- (void)sendMessageToIPhoneNumber:(NSString *)iPhone {
    
    [self openURLToApplicationOpenURLType:kApplicationOpenURLTypeSendMessage withContent:iPhone];
}

- (void)sendEmailToAddress:(NSString *)address {
    
    [self openURLToApplicationOpenURLType:kApplicationOpenURLTypeSendEmail withContent:address];
}

- (void)callPhoneToNumber:(NSString *)number {
    
    [self openURLToApplicationOpenURLType:kApplicationOpenURLTypeCallPhone withContent:number];
}

- (void)openWebToURLString:(NSString *)urlString {
    
    [self openURLToApplicationOpenURLType:kApplicationOpenURLTypeOpenWebResources withContent:urlString];
}

- (void)openURLToApplicationOpenURLType:(kApplicationOpenURLType)urlType withContent:(NSString *)content {
    
    if (!urlType || content.length == 0) {
        [HSYBaseViewController showAlertHUDWithMessage:HSYLOCALIZED(@"没有输入有效的地址")];
        return;
    }
    NSString *prefix = nil;
    switch (urlType) {
        case kApplicationOpenURLTypeOpenAPP: {
            prefix = @"";
        }
            break;
        case kApplicationOpenURLTypeCallPhone: {
            prefix = @"tel://";
        }
            break;
        case kApplicationOpenURLTypeSendMessage: {
            prefix = @"sms://";
        }
            break;
        case kApplicationOpenURLTypeOpenWebResources: {
            prefix = @"http://";
        }
            break;
        case kApplicationOpenURLTypeSendEmail: {
            prefix = @"mailto://";
        }
            break;
            
        default:
            break;
    }
    UIApplication *message = [UIApplication application];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", prefix, content]];
    if ([message canOpenURL:url]) {
        [message openURL:url];
    } else {
        [HSYBaseViewController showAlertHUDWithMessage:HSYLOCALIZED(@"该地址无效")];
    }
}

@end
